package cn.itcast.ecshop.fragment;


import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Toast;

import cn.itcast.ecshop.bean.response.ErrorResponseInfo;
import cn.itcast.ecshop.net.BaseReponse;
import cn.itcast.ecshop.net.BaseRequest;
import cn.itcast.ecshop.net.Callback;
import cn.itcast.ecshop.net.NetUtil;

public abstract class BaseFragment extends Fragment implements Callback<BaseReponse> {

    protected Context mContext;
    private View mRootView;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }


    public void sendRequest(BaseRequest request, Class<? extends BaseReponse> responseClass) {

        NetUtil.sendRequest(request, responseClass, this);
    }

    public abstract <Res extends BaseReponse> void onSuccessResult(BaseRequest request, Res response);

    @Override
    public void onSuccess(BaseRequest request, BaseReponse response) {

        onSuccessResult(request, response);
    }

    @Override
    public void onOther(BaseRequest request, ErrorResponseInfo errorResponseInfo) {

        Toast.makeText(mContext, errorResponseInfo.error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(BaseRequest request, Exception e) {

        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_SHORT).show();
    }

}
